/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ntb.controllers;

import com.ntb.common.SecuredAction;
import com.ntb.controllers.forms.LocationForm;
import com.ntb.entities.Locations;
import com.ntb.models.LocationsDAO;
import com.ntb.models.impl.LocationsDAOImpl;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author Thinh Hung
 */
public class LocationAction extends SecuredAction {

    List<String> errorMessages;
    List<String> successMessages;

    @Override
    public ActionForward ntbExecute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {

        errorMessages = new ArrayList<String>();
        successMessages = new ArrayList<String>();

        ActionForward forward = mapping.getInputForward();
        LocationForm locationForm = (LocationForm) form;
        LocationsDAO locationDao = new LocationsDAOImpl();
        String action = locationForm.getAction();

        if ("save".equalsIgnoreCase(action)) {
            int result = updateLocation(locationForm, locationDao);
            if (result == -1) {
                forward = mapping.findForward("locationListing");
            } else {
                forward = mapping.findForward("locationListing");
            }
        } else if (locationForm.getLocationID() > 0) {
            Locations location = locationDao.getRow(locationForm.getLocationID());

            locationForm.setLocationID(location.getLocationID());
            locationForm.setLocationName(location.getLocationName());
            
            locationForm.setPresentCost("" + location.getPresentCost());

            forward = mapping.findForward("locationEdit");
        } else {
            locationForm.setLocationID(0);
        }

        if (locationForm.getLocationID() > 0) {
            Locations location = locationDao.getRow(locationForm.getLocationID());

            locationForm.setLocationID(location.getLocationID());
            locationForm.setLocationName(location.getLocationName());
            locationForm.setPresentCost("" + location.getPresentCost());
        }

        request.setAttribute("errorMessages", errorMessages);
        request.setAttribute("successMessages", successMessages);

        return forward;

    }

    private int updateLocation(LocationForm locationForm, LocationsDAO locationDao) {
        Locations location = new Locations();

        location.setLocationID(locationForm.getLocationID());
        location.setLocationName(locationForm.getLocationName());
        int presentCost = 0;
        try {
            presentCost = Integer.parseInt(locationForm.getPresentCost());
        } catch (Exception ex) {
        
        }
        location.setPresentCost(presentCost);

        if (location.getLocationID() > 0) {
            Locations temp = locationDao.getRow(location.getLocationID());
            if (temp == null) {
                errorMessages.add("Location not found!");
                return -1;
            } else {

                if (!temp.getLocationName().equalsIgnoreCase(location.getLocationName())) {
                    if (locationDao.checkDuplicateName(location.getLocationName())) {
                        errorMessages.add("The location name you selected is already in use.");
                        return 0;
                    }
                }

                locationDao.update(location);
                successMessages.add("The group \"" + location.getLocationName() + "\" has been saved.");
                return 1;
            }
        } else {
            if (locationDao.checkDuplicateName(location.getLocationName())) {
                errorMessages.add("The location name you selected is already in use.");
                return 0;
            }

            locationDao.add(location);
            successMessages.add("The group \"" + location.getLocationName() + "\" has been added.");
            return 1;
        }
    }
}
